package com.zz.yt.lib.bluetooth.utils;


import android.annotation.SuppressLint;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.ConvertUtils;

import java.text.DecimalFormat;

/**
 * 重量
 *
 * @author qf
 * @version 1.0
 **/
public class QualityUtils {

    private final TextView mTextWeight;

    @NonNull
    public static QualityUtils create(TextView textWeight) {
        return new QualityUtils(textWeight);
    }

    private QualityUtils(TextView textWeight) {
        this.mTextWeight = textWeight;
    }

    /**
     * 获取重量
     *
     * @param bytes:
     */
    @SuppressLint("SetTextI18n")
    public void getResultQuality(byte[] bytes) {
        final String hexString = ConvertUtils.bytes2String(bytes);
        try {
            final String[] split = hexString.split("=");
            final String str = split[1];
            StringBuffer buf = new StringBuffer(str);
            buf = buf.reverse();
            final String spot = ".";
            final DecimalFormat df = new  DecimalFormat("#.00");
            if (buf.toString().contains(spot)) {
                double s = Double.parseDouble(buf.toString());
                df.format(s);
                mTextWeight.setText(s + "");
            } else {
                double s = Integer.parseInt(buf.toString());
                df.format(s);
                double ss = s / 1000;
                df.format(ss);
                mTextWeight.setText(ss + "");
            }
        } catch (Exception e) {
            e.printStackTrace();

        }
    }


}
